<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>4- 阵列立方体</title>
</head>
<style>
    body {
        overflow: hidden;
        margin: 0px;
    }
</style>

<body>

</body>
<script type="importmap">
    {
        "imports": {
            "three": "./build/three.module.js",
            "three/addons/": "./examples/jsm/"
        }
    }
</script>
<script type="module">
    import * as THREE from 'three';
    // 引入轨道控制器扩展库OrbitControls.js
    import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
    import Stats from 'three/addons/libs/stats.module.js';

    const width = window.innerWidth; //宽度
    const height = window.innerHeight; //高度
    console.log(width, height)

    //  场景 相机 渲染器
    const scene = new THREE.Scene()

    const renderer = new THREE.WebGLRenderer();
    renderer.setSize(width, height);

    const camera = new THREE.PerspectiveCamera(80, width / height, 1, 8000);
    camera.position.set(800, 800, 800);
    camera.lookAt(0, 0, 400);
    // camera.lookAt(1000, 0, 1000);

    // 模型
    const geometry = new THREE.BoxGeometry(100, 100, 100);
    const material = new THREE.MeshLambertMaterial({
        color: 0x00ffff, //设置材质颜色
        transparent: true,//开启透明
        opacity: 0.5,//设置透明度
    });
    for (let i = 0; i < 10; i++) {
        for (let j = 0; j < 10; j++) {
            const mesh = new THREE.Mesh(geometry, material); //网格模型对象Mesh
            // 在XOZ平面上分布
            mesh.position.set(i * 200, 0, j * 200);
            scene.add(mesh); //网格模型添加到场景中  
        }
    }

    // 添加辅助坐标轴
    const axesHelper = new THREE.AxesHelper(150);
    scene.add(axesHelper)


    // 平行光
    const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
    directionalLight.position.set(100, 200, 100);
    // directionalLight.target = mesh;
    scene.add(directionalLight);


    renderer.render(scene, camera); //执行渲染操作

    document.body.appendChild(renderer.domElement);

    // 画布跟随窗口变化
    window.onresize = function () {
        renderer.setSize(window.innerWidth, window.innerHeight);
        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        renderer.render(scene, camera); //执行渲染操作
    };

</script>

</html>